﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MagicArena.framework.toon;
using Microsoft.Xna.Framework;
using MagicArena.framework.state;

namespace MagicArena.framework.component
{
    public abstract class AVisionController : IUpdateable, IToonComponent
    {
        #region Properties
        public State Radius
        {
            get;
            private set;
        }
        public Toon[] Visible
        {
            get
            {
                return _Visible.ToArray();
            }
        }
        #endregion

        #region Events
        public delegate void OnVisibleChange(object sender, ToonEventArgs e);
        public class ToonEventArgs : EventArgs
        {
            public Toon T
            {
                get;
                private set;
            }
            public ToonEventArgs(Toon t)
            {
                T = t;
            }
        }
        public event OnVisibleChange Enter, Exit;
        protected virtual void OnEnter(ToonEventArgs e)
        {
            if (Enter != null) Enter(this, e);
        }
        protected virtual void OnExit(ToonEventArgs e)
        {
            if (Exit != null) Exit(this, e);
        }
        #endregion

        #region Private variables
        List<Toon> _Visible = new List<Toon>(5);
        Toon _Source;
        #endregion

        public AVisionController(State radius)
        {
            if (radius.Id != State.Common.Radius) throw new ArgumentException("radius does not have proper Id");
            Radius = radius;
        }

        #region Protected methods
        protected void AddToon(Toon t)
        {
            if(!_Visible.Contains(t)){
                _Visible.Add(t);
                OnEnter(new ToonEventArgs(t));
            }

        }
        protected void RemoveToon(Toon t)
        {
            if (_Visible.Remove(t))
            {
                OnExit(new ToonEventArgs(t));
            }
        }
        #endregion

        #region IUpdatable methods
        #region Enable
        private bool _Enabled = false;
        public bool Enabled
        {
            get { return _Enabled; }
            set
            {
                if (_Enabled != value)
                {
                    _Enabled = value;
                    OnEnabledChanged(new EnabledEventArgs(value));
                }
                _Enabled = value;
            }
        }

        public event EventHandler<EventArgs> EnabledChanged;

        public class EnabledEventArgs : EventArgs
        {
            public bool NewState
            {
                get;
                private set;
            }
            public EnabledEventArgs(bool state)
            {
                NewState = state;
            }
        }
        protected virtual void OnEnabledChanged(EventArgs e)
        {
            if(EnabledChanged !=null) EnabledChanged(this, e);
        }
        #endregion

        #region Update
        public abstract void Update(GameTime gameTime);

        public int UpdateOrder
        {
            get { return Int16.MaxValue / 2; }
        }

        public event EventHandler<EventArgs> UpdateOrderChanged;
        #endregion
        #endregion

        public virtual void AttachToToon(Toon t)
        {
            _Source = t;
        }
    }
}
